package com.nowcoder.community.entity;

import lombok.Data;

/**
 * 分页
 */
public class Page {
    //当前页码
    private int current = 1;//当前页
    //显示上限
    private int limit = 10;//上限
    //数据总数
    private int rows;//页总数
    //查询路径
    private String path;//查询路径，用于分页

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        if (current >= 1){
        this.current = current;
        }
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        if (limit >= 1 && limit <= 100){
            this.limit = limit;
        }
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        if (rows >= 0){
            this.rows = rows;
        }
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    /**
     * 起始页
     * @return
     */
    public int getOffset(){
        return (current - 1) * limit;
    }

    /**
     * 页总数
     * @return
     */
    public int getTotal(){
        if (rows % limit == 0){
            return rows / limit;
        }
        return (rows / limit)+1;
    }

    /**
     * 获取起始页码
     * @return
     */
    public int getFrom(){
        int from = current - 2;
        return from < 1 ? 1 : from;
    }

    /**
     * 获取终止页码
     * @return
     */
    public int getTo(){
        int to = current + 2;
        int total = getTotal();
        return to > total ? total : to;
    }
}
